<template>
  <div class="l-rblock l-form-viewer" style="padding:16px 16px 0 16px">
    <el-form
      :model="formData"
      :rules="rules"
      size="mini"
      ref="form"
    >
      <div class="l-rblock">
        <el-form-item prop="f_Name">
          <el-input
            v-model="formData.f_Name"
            :placeholder="$t('请输入名称')"
          >
          </el-input>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>
<script>
const api = window.$api.system.file
export default {
  data() {
    return {
      moduleCode: "learun-file",
      formData: {
        f_Name: "", // 名称
      },
      rules: {
        f_Name: [
          { required: true, message: this.$t("请输入名称") },
          { 
            validator: this.lr_existDbFiled,
            keyValue:() => { return this.formData.f_Id },
            tableName:'lr_base_folder',
            keyName:'f_Id',
            filedsJsonName:'fileFolderForm_filedsJson',
            message: this.$t("文件夹名重复"),
            trigger: 'null'
          }
        ],
      },
      pid:''
    };
  },
  computed: {
    fileFolderForm_filedsJson(){
      return {f_PId:this.pid,f_DeleteMark:0}
    }

  },
  methods: {
    // 重置表单
    resetForm() {
      this.formData.f_Id = ''
      this.formData.f_PId = ''
      this.pid = ''
      this.$formClear(this.$refs.form)
    },
    // 校验表单
    async validateForm(pid) {
      this.pid = pid
      
      if (!(await this.$formValidateWraper(this.$refs.form))) {
        return false
      }
      return true
    },
    // 设置表单数据
    setForm(data) {
      this.formData = data
    },
    // 获取表单数据
    getForm() {
      return this.$deepClone(this.formData)
    },

    async saveForm({ keyValue,pid,levelId, isEdit /*,code,node*/ }) {
      // isEdit 是否更新数据, keyValue 流程中相当于流程processId,code 表示流程中的操作码,node 流程节点
      const postData = this.getForm()
      if (isEdit) {
        // 编辑
        return await this.$awaitWraper(api.reNameFolder(keyValue, postData.f_Name))
      } else {
        // 新增
        postData.f_PId = pid
        postData.f_LevelId = levelId
        
        postData.f_Id = keyValue
        return await this.$awaitWraper(api.addFolder(postData))
      }
    },
  },
};
</script>